﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;

namespace PosterMaker
{
    /// <summary>
    /// 海报类
    /// </summary>
    public class Poster
    {
        #region 常量
        const string FONT_NAME = "千图马克手写体";

        #endregion // 常量

        #region 属性
        /// <summary>
        /// 海报输出数径
        /// </summary>
        public string OutputPath
        {
            get;
            set;
        }

        /// <summary>
        /// 海报的默认字体类
        /// </summary>
        //private Font _textFont = new Font(FONT_NAME, 14, FontStyle.Bold, GraphicsUnit.Pixel);
        //public Font TextFont
        //{
        //    get
        //    {
        //        return _textFont;
        //    }
        //}

        /// <summary>
        /// 海报的最终效果
        /// </summary>
        public Bitmap PosterImage { get; set; }

        /// <summary>
        /// 海报的底图
        /// </summary>
        public Bitmap BackgroundImage { get; set; }

        public string ImagePath { get; set; }

        /// <summary>
        /// 获取底图的短边长度
        /// </summary>
        public float ShorterLength
        {
            get
            {
                if (BackgroundImage == null)
                    return 0;
                else
                {
                    return BackgroundImage.Width > BackgroundImage.Height ? BackgroundImage.Height : BackgroundImage.Width;
                }
            }
        }

        #endregion // 属性

        #region 构造函数
        /// <summary>
        /// 实例化海报类
        /// </summary>
        /// <param name="imagePath">海报图片的路径</param>
        /// <param name="outputPath">海报输出路径</param>
        public Poster(string imagePath, string outputPath)
        {
            OutputPath = outputPath;
            ImagePath = imagePath;
            PosterImage = new Bitmap(imagePath);
            BackgroundImage = new Bitmap(imagePath);
        }

        /// <summary>
        /// 实例化海报类
        /// </summary>
        /// <param name="imagePath">海报图片的路径</param>
        public Poster(string imagePath)
        {
            OutputPath = Directory.GetCurrentDirectory();
            ImagePath = imagePath;
            PosterImage = new Bitmap(imagePath);
            BackgroundImage = new Bitmap(imagePath);
        }
        #endregion

        #region 方法
        /// <summary>
        /// 添加文字，默认居中
        /// </summary>
        /// <param name="text"></param>
        public void AddText(PosterText text)
        {
            AddText(text, PosterPosition.Center, 1, true);
        }

        /// <summary>
        /// 在指定位置放置海报文字
        /// </summary>
        /// <param name="text">海报文字类</param>
        /// <param name="position">坐标</param>
        /// <param name="y">垂直偏移量，离顶点的距离，80=80%</param>
        public void AddText(PosterText text, PosterPosition position, int y, bool bold)
        {
            // 文本格式类
            StringFormat format = new StringFormat();

            y = y > 100 ? 100 : y;

            int posY = PosterImage.Height * y / 100;

            // 建立该文本的矩形范围，方便后续定位文字位置
            Rectangle textZone = new Rectangle(0, posY, PosterImage.Width, PosterImage.Height - posY);


            // 根据图片的短边长度比例缩放文字的大小
            float size = text.Ratio / 100 * ShorterLength;

            Font textFont = new Font(text.FontName, size, bold ? FontStyle.Bold : FontStyle.Regular, GraphicsUnit.Pixel);
            // 实例化字体类
            //_textFont = new Font(text.FontName, size, FontStyle.Bold, GraphicsUnit.Pixel);
            //float textWidth = GetTextWidth();

            switch (position)
            {
                case PosterPosition.Center:
                    format.Alignment = StringAlignment.Center;
                    break;
                case PosterPosition.Right:
                    format.Alignment = StringAlignment.Far;
                    break;
                default:
                    format.Alignment = StringAlignment.Near;
                    break;
            }


            //AddText(text, x, y);
            using (Graphics g = Graphics.FromImage(PosterImage))
            {
                //g.DrawString(text.Content, _textFont, text.Brush, text.Position);
                g.DrawString(text.Content, textFont, text.Brush, textZone, format);
            }
        }

        /// <summary>
        /// 在海报上画横线
        /// </summary>
        public void AddLine(int x, int y)
        {
            using (Graphics g = Graphics.FromImage(PosterImage))
            {
                using(Pen pen=new Pen(Color.FromArgb(253, 250, 189)))
                {
                    pen.Width = 8F;
                    pen.Alignment = PenAlignment.Inset;
                    g.DrawLine(pen, x, y, x + 100, y);                    
                }
                //g.DrawString(text.Content, _textFont, text.Brush, text.Position);
                
            }
        }

        //private float GetTextWidth()
        //{
        //    SizeF f = new SizeF();
        //    using (Graphics g = Graphics.FromImage(PosterImage))
        //    {
        //        g.PageUnit = GraphicsUnit.Pixel;
        //        f = g.MeasureString(" ", _textFont);
        //    }
        //    return f.Width;
        //}
        #endregion // 方法

        #region 临时方法
        /// <summary>
        /// 合并图片
        /// </summary>
        /// <param name="imgBack"></param>
        /// <param name="img"></param>
        /// <returns></returns>
        public static Bitmap CombinImage(Image imgBack, Image img, int xDeviation = 0, int yDeviation = 0)
        {

            Bitmap bmp = new Bitmap(imgBack.Width, imgBack.Height + img.Height);

            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height); //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);

            //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框

            //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);

            g.DrawImage(img, imgBack.Width / 2 - img.Width / 2 + xDeviation, imgBack.Height + yDeviation, img.Width, img.Height);
            GC.Collect();
            return bmp;
        }

        /// <summary>
        /// Resize图片
        /// </summary>
        /// <param name="bmp">原始Bitmap</param>
        /// <param name="newW">新的宽度</param>
        /// <param name="newH">新的高度</param>
        /// <param name="mode">保留着，暂时未用</param>
        /// <returns>处理以后的图片</returns>
        public static Image ResizeImage(Image bmp, int newW, int newH, int mode)
        {
            try
            {
                Image b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);

                // 插值算法的质量
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height),
                            GraphicsUnit.Pixel);
                g.Dispose();
                return b;
            }
            catch
            {
                return null;
            }
        }

        //MemoryStream ms = new MemoryStream();
        //bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

        #endregion  //临时方法

    }


    public enum PosterPosition
    {
        Center, Left, Right
    }
}
